/**
 * 
 */
package cn.edu.cqu.bysj.wm.ago.lp;

import org.neo4j.graphdb.Direction;
import org.neo4j.graphdb.GraphDatabaseService;

import cn.edu.cqu.bysj.wm.ago.anlys.Cluster;
import cn.edu.cqu.bysj.wm.ago.anlys.ModularityLP;
import cn.edu.cqu.bysj.wm.ago.anlys.Strength;
import cn.edu.cqu.bysj.wm.def.ConstDef;
import cn.edu.cqu.bysj.wm.def.Weight;
import cn.edu.cqu.bysj.wm.gui.result.DetectedResult;
import cn.edu.cqu.bysj.wm.neo4j.Neo4jGraphDb;
import cn.edu.cqu.bysj.wm.util.CommunityOutput;

/**
 * @author Meng
 * 
 */
public class LPRun {

	private static Neo4jGraphDb neo4jGraphDb;
	private static GraphDatabaseService gdb;

	/**
	 * initialize graph database service
	 */
	private static void initDbService() {
		neo4jGraphDb = new Neo4jGraphDb(ConstDef.LP_DB_PATH);
		gdb = neo4jGraphDb.db;
	}

	/**
	 * open graph database service
	 */
	public static GraphDatabaseService openDbService() {
		gdb = neo4jGraphDb.openDb(ConstDef.LP_DB_PATH);
		return gdb;
	}

	/**
	 * close graph database service
	 */
	public static void closeDbService() {
		neo4jGraphDb.shutDownDb();
	}

	/**
	 * run LP algorithm
	 * 
	 * @param filePath
	 * @param weight
	 * @param direction
	 */
	public static void runLP(String filePath, Weight weight, Direction direction) {
		initDbService();

		LP lp = new LP(filePath, gdb, weight, direction);

		lp.interationLP();

		LPCommunity.showCommunity(gdb);

		ModularityLP.calculate(lp);
		Cluster.calculate(direction, ConstDef.LP);
		Strength.judge(direction, ConstDef.LP);

		DetectedResult.setLPAlgorithmResult(lp);
		CommunityOutput.outputLP(lp);

		// closeDbService();
	}
	
}
